package com.demo.web.pc;

import com.demo.common.Constants;
import com.demo.dao.ShareVisitDetailMapper;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.entity.SalePerson;
import com.demo.entity.ShareVisitDetail;
import com.demo.entity.StatisticShare;
import com.demo.service.IStatisticShareService;
import com.demo.vo.StatisticShareVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 统计
 */
@Controller
@RequestMapping("/statistic")
public class StatisticPcController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IStatisticShareService dataService;
    @Autowired
    private ShareVisitDetailMapper svdDao;

    @ResponseBody
    @RequestMapping(value = "/share", method = RequestMethod.POST)
    public BaseResult get(StatisticShare arg, HttpSession session) throws NoSuchAlgorithmException {
        StatisticShare ss = new StatisticShare();
        SalePerson sp = (SalePerson) session.getAttribute("current");
        ss.setLink(arg.getLink());
        arg.setStatus("0");
        arg.setNumber(0);
        if (sp != null) {
            arg.setSharer(String.valueOf(sp.getId()));
        }
        int service_res = dataService.insertSelective(arg);
        BaseResult res = new BaseResult(true, service_res);
        return res;
    }
    @ResponseBody
    @RequestMapping(value = "/visit", method = RequestMethod.GET)
    public BaseResult visit(StatisticShare arg, HttpSession session, HttpServletRequest requset) throws NoSuchAlgorithmException {
        if (arg.getNo() == null) {
            return new BaseResult(false, "", 403);
        }

        StatisticShare ss = dataService.selectByNo(arg.getNo());

        if (ss != null) {
            if (ss.getSharer() != null) {
                session.setAttribute(Constants.SHARE, ss.getSharer());
            }
            ShareVisitDetail svd = new ShareVisitDetail();
            svd.setShareId(arg.getNo());
            svd.setIp(StatisticPcController.getIP(requset));
            svdDao.insertSelective(svd);
            ss.setNumber(ss.getNumber() + 1);
            ss.setStatus("1");
            int service_res = dataService.updateByPrimaryKeySelective(ss);
            return new BaseResult(true, service_res);
        } else {
            return new BaseResult(false, "", 403);
        }
    }


    /**
     *
     * @param arg
     * @param session
     * @return
     * @throws NoSuchAlgorithmException
     */
    @ResponseBody
    @RequestMapping(value = "/queryMyShareData", method = RequestMethod.GET)
    public ListResult<Object> queryMyShareData(StatisticShareVo arg, HttpSession session) throws NoSuchAlgorithmException {


        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new ListResult(false, "请登录!", 401);
        }

//        StatisticShareVo ssv = new StatisticShareVo();
        arg.setSharer(String.valueOf(sp.getId()));
//        ssv.setSt
        List<StatisticShareVo> sslist = dataService.queryMyShareData(arg);
        return new ListResult(true, sslist);
    }


    public static String getIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (!checkIP(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    private static boolean checkIP(String ip) {
        if (ip == null || ip.length() == 0 || "unkown".equalsIgnoreCase(ip)
                || ip.split(".").length != 4) {
            return false;
        }
        return true;
    }
}
